home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / DVIPS_55 / dvips / src / c / dvips < prev    next >
Text File  |  1994-05-06  |  35KB  |  1,082 lines

  1. /*
  2.  *   This is the main routine.
  3.  */
  4. #ifndef DEFRES
  5. #define DEFRES (300)
  6. #endif
  7.  
  8. #include "dvips.h" /* The copyright notice there is included too! */
  9. #ifndef SYSV
  10. extern char *strtok() ; /* some systems don't have this in strings.h */
  11. #endif
  12. #ifdef VMS
  13. #define GLOBAL globaldef
  14. #ifdef __GNUC__
  15. #include "climsgdef.h"    /* created by hand, extracted from STARLET.MLB */
  16.             /* and put in GNU_CC:[INCLUDE.LOCAL]           */
  17. #include "ctype.h"
  18. #include "descrip.h"
  19. #else
  20. #include climsgdef
  21. #include ctype
  22. #include descrip
  23. #endif
  24. #endif
  25. /*
  26.  *   First we define some globals.
  27.  */
  28.       extern char *getenv() ;
  29. #ifdef VMS
  30.     static char ofnme[252],infnme[252],pap[40],thh[20];
  31. #endif
  32. fontdesctype *fonthead ;      /* list of all fonts mentioned so far */
  33. fontdesctype *curfnt ;        /* the currently selected font */
  34. sectiontype *sections ;       /* sections to process document in */
  35. Boolean manualfeed ;          /* manual feed? */
  36. Boolean compressed ;          /* compressed? */
  37. Boolean downloadpspk ;        /* use PK for downloaded PS fonts? */
  38. Boolean safetyenclose ;
  39.                           /* enclose in save/restore for stupid spoolers? */
  40. Boolean removecomments = 1 ;  /* remove comments from included PS? */
  41. Boolean nosmallchars ;        /* disable small char optimization for X4045? */
  42. Boolean cropmarks ;           /* add cropmarks? */
  43. Boolean abspage = 0 ;         /* are page numbers absolute? */
  44. Boolean tryepsf = 0 ;         /* should we try to make it espf? */
  45. Boolean secure = 0 ;          /* make safe for suid */
  46. int collatedcopies = 1 ;      /* how many collated copies? */
  47. int sectioncopies = 1 ;       /* how many times to repeat each section? */
  48. integer pagecopies = 1 ;          /* how many times to repeat each page? */
  49. shalfword linepos = 0 ;       /* where are we on the line being output? */
  50. integer maxpages ;            /* the maximum number of pages */
  51. Boolean notfirst, notlast ;   /* true if a first page was specified */
  52. Boolean evenpages, oddpages ; /* true if doing only even/only odd pages */
  53. Boolean pagelist ;            /* true if using page ranges */
  54. Boolean sendcontrolD ;        /* should we send a control D at end? */
  55. integer firstpage ;           /* the number of the first page if specified */
  56. integer lastpage ;
  57. integer firstseq ;
  58. integer lastseq ;
  59. integer hpapersize, vpapersize ; /* horizontal and vertical paper size */
  60. integer hoff, voff ;          /* horizontal and vertical offsets */
  61. integer maxsecsize ;          /* the maximum size of a section */
  62. integer firstboploc ;         /* where the first bop is */
  63. Boolean sepfiles ;            /* each section in its own file? */
  64. int numcopies ;               /* number of copies of each page to print */
  65. char *oname ;                 /* output file name */
  66. char *iname ;                 /* dvi file name */
  67. char *fulliname ;             /* same, with current working directory */
  68. char *strings ;               /* strings for program */
  69. char *nextstring, *maxstring ; /* string pointers */
  70. FILE *dvifile, *bitfile ;     /* dvi and output files */
  71. quarterword *curpos ;        /* current position in virtual character packet */
  72. quarterword *curlim ;         /* final byte in virtual character packet */
  73. fontmaptype *ffont ;          /* first font in current frame */
  74. real conv ;                   /* conversion ratio, pixels per DVI unit */
  75. real vconv ;                  /* conversion ratio, pixels per DVI unit */
  76. real alpha ;                  /* conversion ratio, DVI unit per TFM unit */
  77. integer mag ;                 /* the magnification of this document */
  78. integer num, den ;            /* the numerator and denominator */
  79. int overridemag ;             /* substitute for mag value in DVI file? */
  80. int actualdpi = DEFRES ;      /* the actual resolution of the printer */
  81. int vactualdpi = DEFRES ;     /* the actual resolution of the printer */
  82. int maxdrift ;                /* max pixels away from true rounded position */
  83. int vmaxdrift ;               /* max pixels away from true rounded position */
  84. char *paperfmt ;              /* command-line paper format */
  85. int landscape = 0 ;           /* landscape mode */
  86. integer fontmem ;             /* memory remaining in printer */
  87. integer pagecount ;           /* page counter for the sections */
  88. integer pagenum ;             /* the page number we currently look at */
  89. long bytesleft ;              /* number of bytes left in raster */
  90. quarterword *raster ;         /* area for raster manipulations */
  91. integer hh, vv ;              /* horizontal and vertical pixel positions */
  92.  
  93. /*-----------------------------------------------------------------------*
  94.  * The PATH definitions cannot be defined on the command line because so many
  95.  * DEFINE's overflow the DCL buffer when using the GNU CC compiler.
  96.  *-----------------------------------------------------------------------*/
  97. #if defined(VMS) && defined(__GNUC__)
  98. #include "vms_gcc_paths.h"
  99. #endif
  100.  
  101.  
  102. char *infont ;                /* is the file we are downloading a font? */
  103. #ifndef PICTPATH
  104. #ifndef __THINK__
  105. #define PICTPATH "."
  106. #else
  107. #define PICTPATH ":"
  108. #endif
  109. #endif
  110.  
  111. #ifdef SEARCH_SUBDIRECTORIES
  112. char *fontsubdirpath = FONTSUBDIRPATH ;
  113. #endif
  114. #ifdef FONTLIB
  115. char *flipath = FLIPATH ;     /* pointer to directories for fli files */
  116. char *fliname = FLINAME ;     /* pointer to names of fli files */
  117. #endif
  118. integer swmem ;               /* font memory in the PostScript printer */
  119. int quiet ;                   /* should we only print errors to stderr? */
  120. int filter ;                  /* act as filter default output to stdout,
  121.                                                default input to stdin? */
  122. int prettycolumn ;            /* the column we are at when running pretty */
  123. int gargc ;                   /* global argument count */
  124. char **gargv ;                /* global argument vector */
  125. int totalpages = 0 ;          /* total number of pages */
  126. Boolean reverse ;             /* are we going reverse? */
  127. Boolean usesPSfonts ;         /* do we use local PostScript fonts? */
  128. Boolean usesspecial ;         /* do we use \special? */
  129. Boolean headers_off ;         /* do we send headers or not? */
  130. Boolean usescolor ;           /* IBM: color - do we use colors? */
  131. char *headerfile ;            /* default header file */
  132. char *warningmsg ;            /* a message to write, if set in config file */
  133. Boolean multiplesects ;       /* more than one section? */
  134. Boolean disablecomments ;     /* should we suppress any EPSF comments? */
  135. char *printer ;               /* what printer to send this to? */
  136. char *mfmode ;                /* default MF mode */
  137. frametype frames[MAXFRAME] ;  /* stack for virtual fonts */
  138. fontdesctype *baseFonts[256] ; /* base fonts for dvi file */
  139. integer pagecost;               /* memory used on the page being prescanned */
  140. int delchar;                    /* characters to delete from prescanned page */
  141. integer fsizetol;               /* max dvi units error for psfile font sizes */
  142. Boolean includesfonts;          /* are fonts used in included psfiles? */
  143. fontdesctype *fonthd[MAXFONTHD];/* list headers for included fonts of 1 name */
  144. int nextfonthd;                 /* next unused fonthd[] index */
  145. char xdig[256];                 /* table for reading hexadecimal digits */
  146. char banner[] = BANNER ;        /* our startup message */
  147. Boolean noenv = 0 ;             /* ignore PRINTER envir variable? */
  148. Boolean dopprescan = 0 ;        /* do we do a scan before the prescan? */
  149. integer lastheadermem ;         /* how much mem did the last header require? */
  150. extern int dontmakefont ;
  151. struct papsiz *papsizes ;       /* all available paper size */
  152. int headersready ;              /* ready to check headers? */
  153. #if defined MSDOS || defined OS2
  154. char *mfjobname = NULL;         /* name of mfjob file if given */
  155. FILE *mfjobfile = NULL;         /* mfjob file for font making instructions */
  156. #endif
  157. #ifdef DEBUG
  158. integer debug_flag = 0;
  159. #endif /* DEBUG */
  160. char queryline[256];                /* interactive query of options */
  161. int qargc;
  162. char *qargv[32];
  163. char queryoptions;
  164. /*
  165.  *   This routine calls the following externals:
  166.  */
  167. extern void outbangspecials() ;
  168. extern void prescanpages() ;
  169. extern void pprescanpages() ;
  170. extern void initprinter() ;
  171. extern void cleanprinter() ;
  172. extern void dosection() ;
  173. extern void getdefaults() ;
  174. extern void cmdout() ;
  175. extern void numout() ;
  176. extern void initcolor() ;
  177. extern int add_header() ;
  178. extern int ParsePages() ;
  179. extern void checkenv() ;
  180. extern void getpsinfo(), revpslists() ;
  181. #ifdef FONTLIB
  182. extern void fliload() ;
  183. #endif
  184. #ifdef __THINK__
  185. int dcommand(char ***);
  186. #endif
  187.  
  188. /* Declare the routine to get the current working directory.  */
  189.  
  190. #ifndef IGNORE_CWD
  191. #ifndef HAVE_GETCWD
  192. extern char *getwd (); /* said to be faster than getcwd (SunOS man page) */
  193. #define getcwd(b, len)  getwd(b) /* used here only when b nonnull */
  194. #else
  195. #ifdef ANSI
  196. extern char *getcwd (char *, int);
  197. #else
  198. extern char *getcwd ();
  199. #endif /* not ANSI */
  200. #endif /* not HAVE_GETWD */
  201. #if defined(SYSV) || defined(VMS) || defined(MSDOS) || defined(OS2)
  202. #define MAXPATHLEN (256)
  203. #else
  204. #include <sys/param.h>          /* for MAXPATHLEN */
  205. #endif
  206. #endif /* not IGNORE_CWD */
  207.  
  208. static char *helparr[] = {
  209. #ifndef VMCMS
  210. "    Usage: dvips [options] filename[.dvi]",
  211. #else
  212. "    VM/CMS Usage:",
  213. "           dvips fname [ftype [fmode]] [options]",
  214. "or",
  215. "           dvips fname[.ftype[.fmode]] [options]",
  216. #endif
  217. "a*  Conserve memory, not time      y # Multiply by dvi magnification",
  218. "b # Page copies, for posters e.g.  A   Print only odd (TeX) pages",
  219. "c # Uncollated copies              B   Print only even (TeX) pages",
  220. "d # Debugging                      C # Collated copies",
  221. "e # Maxdrift value                 D # Resolution",
  222. "f*  Run as filter                  E*  Try to create EPSF",
  223. "h f Add header file                F*  Send control-D at end",
  224. "i*  Separate file per section      K*  Pull comments from inclusions",
  225. "k*  Print crop marks               M*  Don't make fonts",
  226. "l # Last page                      N*  No structured comments",
  227. "m*  Manual feed                    O c Set/change paper offset",
  228. #if defined(MSDOS) || defined(OS2)
  229. "n # Maximum number of pages        P s Load $s.cfg",
  230. #else
  231. "n # Maximum number of pages        P s Load config.$s",
  232. #endif
  233. "o f Output file                    R   Run securely",
  234. "p # First page                     S # Max section size in pages",
  235. "q*  Run quietly                    T c Specify desired page size",
  236. "r*  Reverse order of pages         U*  Disable string param trick",
  237. "s*  Enclose output in save/restore V*  Send downloadable PS fonts as PK",
  238. "t s Paper format                   X # Horizontal resolution",
  239. "x # Override dvi magnification     Y # Vertical resolution",  
  240. "                                   Z*  Compress bitmap fonts",
  241. /* "-   Interactive query of options", */
  242. "    # = number   f = file   s = string  * = suffix, `0' to turn off",
  243. "    c = comma-separated dimension pair (e.g., 3.2in,-32.1cm)", 0} ;
  244. void help() {
  245.    char **p ;
  246.    for (p=helparr; *p; p++)
  247.       fprintf(stderr, " %s\n", *p) ;
  248. }
  249. /*
  250.  *   This error routine prints an error message; if the first
  251.  *   character is !, it aborts the job.
  252.  */
  253. static char *progname ;
  254. void
  255. error(s)
  256.         char *s ;
  257. {
  258.    extern void exit() ;
  259.  
  260.    if (prettycolumn > 0)
  261.         fprintf(stderr,"\n");
  262.    prettycolumn = 0;
  263.    (void)fprintf(stderr, "%s: %s\n", progname, s) ;
  264.    if (*s=='!') {
  265.       if (bitfile != NULL) {
  266.          cleanprinter() ;
  267.       }
  268.       exit(1) ; /* fatal */
  269.    }
  270. }
  271. /*
  272.  *   This is our malloc that checks the results.  We debug the
  273.  *   allocations but not the frees, since memory fragmentation
  274.  *   might be such that we can never use the free'd memory and
  275.  *   it's wise to be conservative.  The only real place we free
  276.  *   is when repacking *huge* characters anyway.
  277.  */
  278. #ifdef DEBUG
  279. static integer totalalloc = 0 ;
  280. #endif
  281. char *mymalloc(n)
  282. integer n ;
  283. {
  284.    char *p ;
  285.  
  286. #ifdef SMALLMALLOC
  287.    if (n > 65500L)
  288.       error("! can't allocate more than 64K!") ;
  289. #endif
  290.    if (n <= 0) /* catch strange 0 mallocs in flib.c without breaking code */
  291.       n = 1 ;
  292. #ifdef DEBUG
  293.    totalalloc += n ;
  294.    if (dd(D_MEM)) {
  295. #ifdef SHORTINT
  296.       fprintf(stderr, "Alloc %ld\n", n) ;
  297. #else
  298.       fprintf(stderr, "Alloc %d\n", n) ;
  299. #endif
  300.    }
  301. #endif
  302.    p = malloc(n) ;
  303.    if (p == NULL)
  304.       error("! no memory") ;
  305.    return p ;
  306. }
  307. void
  308. morestrings() {
  309.    strings = mymalloc((integer)STRINGSIZE) ;
  310.    nextstring = strings ;
  311.    maxstring = strings + STRINGSIZE - 200 ;
  312.    *nextstring++ = 0 ;
  313. }
  314. void
  315. checkstrings() {
  316.    if (nextstring - strings > STRINGSIZE / 2)
  317.       morestrings() ;
  318. }
  319. /*
  320.  *   Initialize sets up all the globals and data structures.
  321.  */
  322. void
  323. initialize()
  324. {
  325.    int i;
  326.    char *s;
  327.  
  328.    nextfonthd = 0;
  329.    for (i=0; i<256; i++)
  330.       xdig[i] = 0;
  331.    i = 0;
  332.    for (s="0123456789ABCDEF"; *s!=0; s++)
  333.       xdig[(int)*s] = i++;
  334.    i = 10;
  335.    for (s="abcdef"; *s!=0; s++)
  336.       xdig[(int)*s] = i++;
  337.    morestrings() ;
  338.    maxpages = 100000 ;
  339.    numcopies = 1 ;
  340.    iname = fulliname = strings ;
  341.    bitfile = NULL ;
  342.    bytesleft = 0 ;
  343.    swmem = SWMEM ;
  344.    oname = OUTPATH ;
  345.    sendcontrolD = 0 ;
  346.    multiplesects = 0 ;
  347.    disablecomments = 0 ;
  348.    maxdrift = -1 ;
  349.    vmaxdrift = -1 ;
  350. }
  351. /*
  352.  *   This routine copies a string into the string `pool', safely.
  353.  */
  354. char *
  355. newstring(s)
  356.    char *s ;
  357. {
  358.    int l ;
  359.  
  360.    if (s == NULL)
  361.       return(NULL) ;
  362.    l = strlen(s) ;
  363.    if (nextstring + l >= maxstring)
  364.       morestrings() ;
  365.    if (nextstring + l >= maxstring)
  366.       error("! out of string space") ;
  367.    (void)strcpy(nextstring, s) ;
  368.    s = nextstring ;
  369.    nextstring += l + 1 ;
  370.    return(s) ;
  371. }
  372. void newoutname() {
  373.    static int seq = 0 ;
  374.    static char *seqptr = 0 ;
  375.    char *p ;
  376.  
  377.    if (oname == 0 || *oname == 0)
  378.       error("! need an output file name to specify separate files") ;
  379.    if (*oname != '!' && *oname != '|') {
  380.       if (seqptr == 0) {
  381.          oname = newstring(oname) ;
  382.          seqptr = 0 ;
  383.          for (p = oname; *p; p++)
  384.             if (*p == '.')
  385.                seqptr = p + 1 ;
  386.          if (seqptr == 0)
  387.             seqptr = p ;
  388.          nextstring += 5 ; /* make room for the number, up to five digits */
  389.       }
  390.       sprintf(seqptr, "%03d", ++seq) ;
  391.    }
  392. }
  393. /*
  394.  *   This routine reverses a list, where a list is defined to be any
  395.  *   structure whose first element is a pointer to another such structure.
  396.  */
  397. VOID *revlist(p)
  398. VOID *p ;
  399. {
  400.    struct list {
  401.       struct list *next ;
  402.    } *pp = (struct list *)p, *qq = 0, *tt ;
  403.  
  404.    while (pp) {
  405.       tt = pp->next ;
  406.       pp->next = qq ;
  407.       qq = pp ;
  408.       pp = tt ;
  409.    }
  410.    return (VOID *)qq ;
  411. }
  412. /* this asks for a new set of arguments from the command line */
  413. void
  414. queryargs()
  415. {
  416.    fputs("Options: ",stdout);
  417.    fgets(queryline,256,stdin);
  418.    qargc=1;
  419.    if ( (qargv[1] = strtok(queryline," \n")) != (char *)NULL ) {
  420.       qargc=2;
  421.       while ( ((qargv[qargc] = strtok((char *)NULL," \n")) != (char *)NULL)
  422.             && (qargc < 31) )
  423.          qargc++;
  424.    }
  425.    qargv[qargc] = (char *)NULL;
  426. }
  427.  
  428. /*
  429.  *   Finally, our main routine.
  430.  */
  431. extern void handlepapersize() ;
  432. #ifdef VMS
  433. main()
  434. #else
  435. void main(argc, argv)
  436.         int argc ;
  437.         char *argv[] ;
  438. #endif
  439. {
  440.    extern void exit() ;
  441.    int i, lastext = -1 ;
  442. #ifdef MVSXA
  443.    int firstext = -1 ;
  444. #endif
  445.    register sectiontype *sects ;
  446.  
  447. #ifdef __THINK__
  448.    argc = dcommand(&argv) ; /* do I/O stream redirection */
  449. #endif
  450. #ifdef VMS        /* Grab the command-line buffer */
  451.    short len_arg;
  452.    $DESCRIPTOR( verb_dsc, "DVIPS ");    /* assume the verb is always DVIPS */
  453.    struct dsc$descriptor_d temp_dsc = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0};
  454.  
  455.    progname = &thh[0] ;
  456.    strcpy(progname,"DVIPS%ERROR");
  457.  
  458.    lib$get_foreign( &temp_dsc, 0, &len_arg, 0);    /* Get the command line */
  459.    str$prefix(&temp_dsc, &verb_dsc);        /* prepend the VERB     */
  460.    len_arg += verb_dsc.dsc$w_length;        /* update the length    */
  461.    temp_dsc.dsc$a_pointer[len_arg] = '\0';    /* terminate the string */
  462.    gargv = &temp_dsc.dsc$a_pointer;        /* point to the buffer  */
  463.    gargc = 1 ;                    /* only one big argv    */
  464. #else
  465.    progname = argv[0] ;
  466.    gargv = argv ;
  467.    gargc = argc ;
  468. /* we sneak a look at the first arg in case it's debugging */
  469. #ifdef DEBUG
  470.    if (argc > 1 && strncmp(argv[1], "-d", 2)==0) {
  471.       if (argv[1][2]==0) {
  472.          if (sscanf(argv[2], "%d", &debug_flag)==0)
  473.             debug_flag = 0 ;
  474.       } else {
  475.          if (sscanf(argv[1]+2, "%d", &debug_flag)==0)
  476.             debug_flag = 0 ;
  477.       }
  478.    }
  479. #endif
  480. #endif
  481.    initialize() ;
  482.    checkenv(0) ;
  483.    getdefaults(CONFIGFILE) ;
  484.    getdefaults((char *)0) ;
  485. /*
  486.  *   This next whole big section of code is straightforward; we just scan
  487.  *   the options.  An argument can either immediately follow its option letter
  488.  *   or be separated by spaces.  Any argument not preceded by '-' and an
  489.  *   option letter is considered a file name; the program complains if more
  490.  *   than one file name is given, and uses stdin if none is given.
  491.  */
  492. #ifdef VMS
  493. vmscli();
  494. #else
  495.    queryoptions = 0;
  496.    do
  497.    {
  498.       for (i=1; i<argc; i++) {
  499.          if (*argv[i]=='-') {
  500.             char *p=argv[i]+2 ;
  501.             char c=argv[i][1] ;
  502.             switch (c) {
  503. case '-':
  504.                queryoptions = 1;
  505.                break;
  506. case 'a':
  507.                dopprescan = (*p != '0') ;
  508.                break ;
  509. case 'b':
  510.                if (*p == 0 && argv[i+1])
  511.                   p = argv[++i] ;
  512.                if (sscanf(p, "%d", &pagecopies)==0)
  513.                   error("! Bad number of page copies option (-b).") ;
  514.                if (pagecopies < 1 || pagecopies > 1000)
  515.                   error("! can only print one to a thousand page copies") ;
  516.                break ;
  517. case 'c' :
  518.                if (*p == 0 && argv[i+1])
  519.                   p = argv[++i] ;
  520.                if (sscanf(p, "%d", &numcopies)==0)
  521.                   error("! Bad number of copies option (-c).") ;
  522.                break ;
  523. case 'd' :
  524. #ifdef DEBUG
  525.                if (*p == 0 && argv[i+1])
  526.                   p = argv[++i];
  527.                if (sscanf(p, "%d", &debug_flag)==0)
  528.                   error("! Bad debug option (-d).");
  529.                break;
  530. #else
  531.                error("not compiled in debug mode") ;
  532.                break ;
  533. #endif /* DEBUG */
  534. case 'e' :
  535.                if (*p == 0 && argv[i+1])
  536.                   p = argv[++i] ;
  537.                if (sscanf(p, "%d", &maxdrift)==0 || maxdrift<0)
  538.                   error("! Bad maxdrift option (-e).") ;
  539.                vmaxdrift = maxdrift;
  540.                break ;
  541. case 'f' :
  542.                filter = (*p != '0') ;
  543.                if (filter)
  544.                   oname = "" ;
  545.                noenv = 1 ;
  546.                sendcontrolD = 0 ;
  547.                break ;
  548. case 'h' : case 'H' :
  549.                if (*p == 0 && argv[i+1])
  550.                   p = argv[++i] ;
  551.                if (strcmp(p, "-") == 0)
  552.                   headers_off = 1 ;
  553.                else
  554.                   (void)add_header(p) ;
  555.                break ;
  556. case 'i':
  557.                sepfiles = (*p != '0') ;
  558.                break ;
  559. case 'k':
  560.                cropmarks = (*p != '0') ;
  561.                break ;
  562. case 'R':
  563.                secure = 1 ;
  564.                break ;
  565. case 'S':
  566.                if (*p == 0 && argv[i+1])
  567.                   p = argv[++i] ;
  568.                if (sscanf(p, "%d", &maxsecsize)==0)
  569.                   error("! Bad section size arg (-S).") ;
  570.                break ;
  571. case 'm' :
  572.                manualfeed = (*p != '0') ;
  573.                break ;
  574. case 'n' :
  575.                if (*p == 0 && argv[i+1])
  576.                   p = argv[++i] ;
  577. #ifdef SHORTINT
  578.                if (sscanf(p, "%ld", &maxpages)==0)
  579. #else        /* ~SHORTINT */
  580.                if (sscanf(p, "%d", &maxpages)==0)
  581. #endif        /* ~SHORTINT */
  582.                   error("! Bad number of pages option (-n).") ;
  583.                break ;
  584. case 'o' :
  585.                if (*p == 0 && argv[i+1] && *argv[i+1]!='-')
  586.                   p = argv[++i] ;
  587.                oname = p ;
  588.                noenv = 1 ;
  589.                sendcontrolD = 0 ;
  590.                break ;
  591. case 'O' :
  592.                if (*p == 0 && argv[i+1])
  593.                   p = argv[++i] ;
  594.                handlepapersize(p, &hoff, &voff) ;
  595.                break ;
  596. case 'T' :
  597.                if (*p == 0 && argv[i+1])
  598.                   p = argv[++i] ;
  599.                handlepapersize(p, &hpapersize, &vpapersize) ;
  600.                if (landscape) {
  601.                   error(
  602.               "both landscape and papersize specified; ignoring landscape") ;
  603.                   landscape = 0 ;
  604.                }
  605.                break ;
  606. case 'p' :
  607. #if defined MSDOS || defined OS2
  608.                /* check for emTeX job file (-pj=filename) */
  609.                if (*p == 'j') {
  610.                  p++;
  611.                  if (*p == '=')
  612.                    p++;
  613.                  mfjobname = newstring(p);
  614.                  break;
  615.                }
  616.                /* must be page number instead */
  617. #endif
  618.                if (*p == 'p') {  /* a -pp specifier for a page list? */
  619.                   p++ ;
  620.                   if (*p == 0 && argv[i+1])
  621.                      p = argv[++i] ;
  622.                   if (ParsePages(p))
  623.                      error("! Bad page list specifier (-pp).") ;
  624.                   pagelist = 1 ;
  625.                   break ;
  626.                }
  627.                if (*p == 0 && argv[i+1])
  628.                   p = argv[++i] ;
  629.                if (*p == '=') {
  630.                   abspage = 1 ;
  631.                   p++ ;
  632.                }
  633. #ifdef SHORTINT
  634.                switch(sscanf(p, "%ld.%ld", &firstpage, &firstseq)) {
  635. #else        /* ~SHORTINT */
  636.                switch(sscanf(p, "%d.%d", &firstpage, &firstseq)) {
  637. #endif        /* ~SHORTINT */
  638. case 1:           firstseq = 0 ;
  639. case 2:           break ;
  640. default:
  641.                   error("! Bad first page option (-p).") ;
  642.                }
  643.                notfirst = 1 ;
  644.                break ;
  645. case 'l':
  646.                if (*p == 0 && argv[i+1])
  647.                   p = argv[++i] ;
  648.                if (*p == '=') {
  649.                   abspage = 1 ;
  650.                   p++ ;
  651.                }
  652. #ifdef SHORTINT
  653.                switch(sscanf(p, "%ld.%ld", &lastpage, &lastseq)) {
  654. #else        /* ~SHORTINT */
  655.                switch(sscanf(p, "%d.%d", &lastpage, &lastseq)) {
  656. #endif        /* ~SHORTINT */
  657. case 1:           lastseq = 0 ;
  658. case 2:           break ;
  659. default:
  660.                   error("! Bad last page option (-p).") ;
  661.                }
  662.                notlast = 1 ;
  663.                break ;
  664. case 'A':
  665.                oddpages = 1 ;
  666.                break ;
  667. case 'B':
  668.                evenpages = 1 ;
  669.                break ;
  670. case 'q' : case 'Q' :
  671.                quiet = (*p != '0') ;
  672.                break ;
  673. case 'r' :
  674.                reverse = (*p != '0') ;
  675.                break ;
  676. case 't' :
  677.                if (*p == 0 && argv[i+1])
  678.                   p = argv[++i] ;
  679.                if (strcmp(p, "landscape") == 0) {
  680.                   if (hpapersize || vpapersize)
  681.                      error(
  682.              "both landscape and papersize specified; ignoring landscape") ;
  683.                   else
  684.                      landscape = 1 ;
  685.                } else
  686.                   paperfmt = p ;
  687.                break ;
  688. case 'x' : case 'y' :
  689.                if (*p == 0 && argv[i+1])
  690.                   p = argv[++i] ;
  691.                if (sscanf(p, "%d", &mag)==0 || mag < 10 ||
  692.                           mag > 100000)
  693.                   error("! Bad magnification parameter (-x).") ;
  694.                overridemag = (c == 'x' ? 1 : -1) ;
  695.                break ;
  696. case 'C' :
  697.                if (*p == 0 && argv[i+1])
  698.                   p = argv[++i] ;
  699.                if (sscanf(p, "%d", &collatedcopies)==0)
  700.                   error("! Bad number of collated copies option (-C).") ;
  701.                break ;
  702. case 'D' :
  703.                if (*p == 0 && argv[i+1])
  704.                   p = argv[++i] ;
  705.                if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  706.                           actualdpi > 10000)
  707.                   error("! Bad dpi parameter (-D).") ;
  708.                vactualdpi = actualdpi;
  709.                break ;
  710. case 'E' :
  711.                tryepsf = (*p != '0') ;
  712.                break ;
  713. case 'K' :
  714.                removecomments = (*p != '0') ;
  715.                break ;
  716. case 'U' :
  717.                nosmallchars = (*p != '0') ;
  718.                break ;
  719. case 'X' :
  720.                if (*p == 0 && argv[i+1])
  721.                   p = argv[++i] ;
  722.                if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  723.                           actualdpi > 10000)
  724.                   error("! Bad dpi parameter (-D).") ;
  725.                break ;
  726. case 'Y' :
  727.                if (*p == 0 && argv[i+1])
  728.                   p = argv[++i] ;
  729.                if (sscanf(p, "%d", &vactualdpi)==0 || vactualdpi < 10 ||
  730.                           vactualdpi > 10000)
  731.                   error("! Bad dpi parameter (-D).") ;
  732.                vactualdpi = vactualdpi;
  733.                break ;
  734. case 'F' :
  735.                sendcontrolD = (*p != '0') ;
  736.                break ;
  737. case 'M':
  738.                dontmakefont = (*p != '0') ;
  739.                break ;
  740. case 'N' :
  741.                disablecomments = (*p != '0') ;
  742.                break ;
  743. case 'P' :
  744.                if (*p == 0 && argv[i+1])
  745.                   p = argv[++i] ;
  746.                printer = p ;
  747.                noenv = 1 ;
  748.                getdefaults("") ;
  749.                break ;
  750. case 's' :
  751.                safetyenclose = (*p != '0') ;
  752.                break ;
  753. case 'V':
  754.                downloadpspk = (*p != '0') ;
  755.                break ;
  756. case 'Z' :
  757.                compressed = (*p != '0') ;
  758.                break ;
  759. case '?' :
  760.                (void)fprintf(stderr, banner) ;
  761.                help() ;
  762.                break ;
  763. default:
  764.                error(
  765.               "! Bad option, not one of acdefhiklmnopqrstxCDEFKMNOPSTUXYZ?") ;
  766.             }
  767.          } else {
  768.             if (*iname == 0) {
  769.                register char *p ;
  770.    
  771.                lastext = 0 ;
  772.                iname = nextstring ;
  773.                p = argv[i] ;
  774.                while (*p) {
  775.                   *nextstring = *p++ ;
  776.                   if (*nextstring == '.')
  777.                      lastext = nextstring - iname ;
  778.                   else if (*nextstring == '/' || *nextstring == ':')
  779.                      lastext = 0 ;
  780.                   nextstring++ ;
  781.                }
  782.                *nextstring++ = '.' ;
  783.                *nextstring++ = 'd' ;
  784.                *nextstring++ = 'v' ;
  785.                *nextstring++ = 'i' ;
  786.                *nextstring++ = 0 ;
  787.             } else
  788.                error("! Two input file names specified.") ;
  789.          }
  790.       }
  791.       if (noenv == 0) {
  792.          register char *p ;
  793.    
  794.          if (0 != (p = getenv("PRINTER"))) {
  795. #if defined(MSDOS) || defined(OS2)
  796.             strcpy(nextstring, p) ;
  797.             strcat(nextstring, ".cfg") ;
  798. #else
  799.             strcpy(nextstring, "config.") ;
  800.             strcat(nextstring, p) ;
  801. #endif
  802.             getdefaults(nextstring) ;
  803.          }
  804.       }
  805.       if (queryoptions != 0) {            /* get new options */
  806.          (void)fprintf(stderr, banner) ;
  807.          help() ;
  808.          queryargs();
  809.          if (qargc == 1)
  810.            queryoptions = 0;
  811.          else {
  812.            qargv[0] = argv[0];
  813.            argc=qargc;
  814.            argv=qargv;
  815.          }
  816.       }
  817.    } while (queryoptions != 0) ;
  818. #endif
  819.    checkenv(1) ;
  820. /*
  821.  *   The logic here is a bit convoluted.  Since all `additional'
  822.  *   PostScript font information files are loaded *before* the master
  823.  *   one, and yet they should be able to override the master one, we
  824.  *   have to add the information in the master list to the *ends* of
  825.  *   the hash chain.  We do this by reversing the lists, adding them
  826.  *   to the front, and then reversing them again.
  827.  */
  828.    revpslists() ;
  829.    getpsinfo((char *)NULL) ;
  830.    revpslists() ;
  831.    if (!quiet)
  832.       (void)fprintf(stderr, banner) ;
  833.    if (*iname) {
  834.       dvifile = fopen(iname, READBIN) ;
  835. /*
  836.  *   Allow names like a.b.
  837.  */
  838.       if (dvifile == 0) {
  839.          iname[strlen(iname)-4] = 0 ; /* remove the .dvi suffix */
  840.          dvifile = fopen(iname, READBIN) ;
  841.       }
  842.    }
  843.    if (oname[0] == '-' && oname[1] == 0)
  844.       oname[0] = 0 ;
  845.    if (*oname == 0 && ! filter) {
  846.       oname = nextstring ;
  847. #ifndef VMCMS  /* get stuff before LAST "." */
  848.       lastext = strlen(iname) - 1 ;
  849.       while (iname[lastext] != '.' && lastext > 0)
  850.          lastext-- ;
  851.       if (iname[lastext] != '.')
  852.          lastext = strlen(iname) - 1 ;
  853. #else   /* for VM/CMS we take the stuff before FIRST "." */
  854.       lastext = strchr(iname,'.') - iname ;
  855.       if ( lastext <= 0 )     /* if no '.' in "iname" */
  856.          lastext = strlen(iname) -1 ;
  857. #endif
  858. #ifdef MVSXA /* IBM: MVS/XA */
  859.       if (strchr(iname, '(') != NULL  &&
  860.           strchr(iname, ')') != NULL) {
  861.       firstext = strchr(iname, '(') - iname + 1;
  862.       lastext = strrchr(iname, ')') - iname - 1;
  863.          }
  864.       else {
  865.       if (strrchr(iname, '.') != NULL) {
  866.       lastext = strrchr(iname, '.') - iname - 1;
  867.            }
  868.          else lastext = strlen(iname) - 1 ;
  869.       if (strchr(iname, '\'') != NULL)
  870.          firstext = strchr(iname, '.') - iname + 1;
  871.          else firstext = 0;
  872.       }
  873. #endif  /* IBM: MVS/XA */
  874. #ifdef MVSXA /* IBM: MVS/XA */
  875.       for (i=firstext; i<=lastext; i++)
  876. #else
  877.       for (i=0; i<=lastext; i++)
  878. #endif
  879.          *nextstring++ = iname[i] ;
  880.       if (iname[lastext] != '.')
  881.          *nextstring++ = '.' ;
  882. #ifndef VMCMS
  883.       *nextstring++ = 'p' ;
  884.       *nextstring++ = 's' ;
  885. #else  /* might as well keep things uppercase */
  886.       *nextstring++ = 'P' ;
  887.       *nextstring++ = 'S' ;
  888. #endif
  889.       *nextstring++ = 0 ;
  890. /*
  891.  *   Now we check the name, and `throw away' any prefix information.
  892.  *   This means throwing away anything before (and including) a colon
  893.  *   or slash.
  894.  */
  895.       {
  896.          char *p ;
  897.  
  898.          for (p=oname; *p && p[1]; p++)
  899.             if (*p == ':' || *p == DIRSEP)
  900.                oname = p + 1 ;
  901.       }
  902.    }
  903. #ifdef DEBUG
  904.    if (dd(D_PATHS)) {
  905. #ifdef SHORTINT
  906.         (void)fprintf(stderr,"input file %s output file %s swmem %ld\n",
  907. #else /* ~SHORTINT */
  908.            (void)fprintf(stderr,"input file %s output file %s swmem %d\n",
  909. #endif /* ~SHORTINT */
  910.            iname, oname, swmem) ;
  911.    (void)fprintf(stderr,"tfm path %s\npk path %s\n", tfmpath, pkpath) ;
  912.    (void)fprintf(stderr,"fig path %s\nvf path %s\n", figpath, vfpath) ;
  913.    (void)fprintf(stderr,"config path %s\nheader path %s\n",
  914.                   configpath, headerpath) ;
  915. #ifdef FONTLIB
  916.    (void)fprintf(stderr,"fli path %s\nfli names %s\n", flipath, fliname) ;
  917. #endif
  918.    } /* dd(D_PATHS) */
  919. #endif /* DEBUG */
  920. /*
  921.  *   Now we try to open the dvi file.
  922.  */
  923.    if (warningmsg)
  924.       error(warningmsg) ;
  925.    headersready = 1 ;
  926.    headerfile = (compressed? CHEADERFILE : HEADERFILE) ;
  927.    (void)add_header(headerfile) ;
  928.    if (*iname != 0) {
  929.       fulliname = nextstring ;
  930. #ifndef IGNORE_CWD
  931.       if (*iname != '/') {
  932.         getcwd(nextstring, MAXPATHLEN + 2);
  933.         while (*nextstring++) ;
  934. #ifdef VMS        /* VMS doesn't need the '/' character appended */
  935.         nextstring--;    /* so just back up one byte. */
  936. #else
  937.         *(nextstring-1) = '/' ;
  938. #endif
  939.       }
  940. #endif
  941.       strcpy(nextstring,iname) ;
  942.       while (*nextstring++) ; /* advance nextstring past fulliname */
  943.    } else if (filter)
  944.       dvifile = stdin;
  945.    else {
  946.       help() ;
  947.       exit(0) ;
  948.    }
  949.    initcolor() ;
  950.    if (dvifile==NULL) {
  951.       extern char errbuf[];
  952.       (void)sprintf(errbuf,"! DVI file <%s> can't be opened.", iname) ;
  953.       error("! DVI file can't be opened.") ;
  954.    }
  955.    if (fseek(dvifile, 0L, 0) < 0)
  956.       error("! DVI file must not be a pipe.") ;
  957. #ifdef FONTLIB
  958.    fliload();    /* read the font libaries */
  959. #endif
  960. /*
  961.  *   Now we do our main work.
  962.  */
  963.    swmem += fontmem ;
  964.    if (maxdrift < 0) {
  965.       if (actualdpi <= 599)
  966.          maxdrift = actualdpi / 100 ;
  967.       else if (actualdpi < 1199)
  968.          maxdrift = actualdpi / 200 + 3 ;
  969.       else
  970.          maxdrift = actualdpi / 400 + 6 ;
  971.    }
  972.    if (vmaxdrift < 0) {
  973.       if (vactualdpi <= 599)
  974.          vmaxdrift = vactualdpi / 100 ;
  975.       else if (vactualdpi < 1199)
  976.          vmaxdrift = vactualdpi / 200 + 3 ;
  977.       else
  978.          vmaxdrift = vactualdpi / 400 + 6 ;
  979.    }
  980.    if (dopprescan)
  981.       pprescanpages() ;
  982.    prescanpages() ;
  983. #if defined MSDOS || defined OS2
  984.    if (mfjobfile != (FILE*)NULL) {
  985.      char answer[5];
  986.      fputs("}\n",mfjobfile);
  987.      fclose(mfjobfile);
  988.      fputs("Exit to make missing fonts now (y/n)? ",stdout);
  989.      fgets(answer,4,stdin);
  990.      if (*answer=='y' || *answer=='Y')
  991.        exit(8); /* exit with errorlevel 8 for emTeX dvidrv */
  992.    }
  993. #endif
  994.    if (includesfonts)
  995.       (void)add_header(IFONTHEADER) ;
  996.    if (usesPSfonts)
  997.       (void)add_header(PSFONTHEADER) ;
  998.    if (usesspecial)
  999.       (void)add_header(SPECIALHEADER) ;
  1000.    if (usescolor)  /* IBM: color */
  1001.       (void)add_header(COLORHEADER) ;
  1002.    papsizes = (struct papsiz *)revlist((void *)papsizes) ;
  1003.    sects = sections ;
  1004.    totalpages *= collatedcopies ;
  1005.    if (sects == NULL || sects->next == NULL) {
  1006.       sectioncopies = collatedcopies ;
  1007.       collatedcopies = 1 ;
  1008.    } else {
  1009.       if (! sepfiles)
  1010.          multiplesects = 1 ;
  1011.    }
  1012.    totalpages *= pagecopies ;
  1013.    if (tryepsf) {
  1014.       if (totalpages != 1 || paperfmt || landscape || manualfeed ||
  1015.           collatedcopies > 1 || numcopies > 1 || cropmarks ||
  1016.           *iname == 0) {
  1017.          error("Can't make it EPSF, sorry") ;
  1018.          tryepsf = 0 ;
  1019.       }
  1020.    }
  1021.    if (! sepfiles) {
  1022.       initprinter(0) ;
  1023.       outbangspecials() ;
  1024.    }
  1025.    for (i=0; i<collatedcopies; i++) {
  1026.       sects = sections ;
  1027.       while (sects != NULL) {
  1028.          if (sepfiles) {
  1029.             newoutname() ;
  1030.             if (! quiet) {
  1031.                if (prettycolumn + strlen(oname) + 6 > STDOUTSIZE) {
  1032.                   fprintf(stderr, "\n") ;
  1033.                   prettycolumn = 0 ;
  1034.                }
  1035.                (void)fprintf(stderr, "(-> %s) ", oname) ;
  1036.                prettycolumn += strlen(oname) + 6 ;
  1037.             }
  1038.             initprinter(sects->numpages) ;
  1039.             outbangspecials() ;
  1040.          } else if (! quiet) {
  1041.             if (prettycolumn > STDOUTSIZE) {
  1042.                fprintf(stderr, "\n") ;
  1043.                prettycolumn = 0 ;
  1044.             }
  1045.             (void)fprintf(stderr, ". ") ;
  1046.             prettycolumn += 2 ;
  1047.          }
  1048.          (void)fflush(stderr) ;
  1049.          dosection(sects, sectioncopies) ;
  1050.          sects = sects->next ;
  1051.          if (sepfiles)
  1052.             cleanprinter() ;
  1053.       }
  1054.    }
  1055.    if (! sepfiles)
  1056.       cleanprinter() ;
  1057.    if (! quiet)
  1058.       (void)fprintf(stderr, "\n") ;
  1059. #ifdef DEBUG
  1060.    if (dd(D_MEM)) {
  1061. #ifdef SHORTINT
  1062.       fprintf(stderr, "Total memory allocated:  %ld\n", totalalloc) ;
  1063. #else
  1064.       fprintf(stderr, "Total memory allocated:  %d\n", totalalloc) ;
  1065. #endif
  1066.    }
  1067. #endif
  1068.    exit(0) ;
  1069.    /*NOTREACHED*/
  1070. }
  1071. #ifdef VMS
  1072. #include "[]vmscli.c"
  1073. #endif
  1074.  
  1075. #ifdef VMCMS  /* IBM: VM/CMS */
  1076. #include "dvipscms.h"
  1077. #endif
  1078.  
  1079. #ifdef MVSXA  /* IBM: MVS/XA */
  1080. #include "dvipsmvs.h"
  1081. #endif
  1082.